package cs304.p3.library.controller;

import java.util.List;

import cs304.p3.library.domain.Book;
import cs304.p3.library.domain.Borrower;
import cs304.p3.library.exceptions.AbstractLDBException;
import cs304.p3.library.framework.IoC;
import cs304.p3.library.model.BorrowerLoginForm;
import cs304.p3.library.model.BorrowerSearchForm;

public class BorrowerController extends Controller{
	
	private BorrowerSearchForm borrowerSearchForm = new BorrowerSearchForm();
	private BorrowerLoginForm borrowerLoginForm = new BorrowerLoginForm();
	private Borrower borrower = new Borrower();
	
	public Borrower getBorrower(){
		return borrower;
	}
	
	public BorrowerSearchForm getBorrowerSearchForm(){
		return borrowerSearchForm;
	}
	
	public BorrowerLoginForm getBorrowerLoginForm(){
		return borrowerLoginForm;
	}
	
	public void performSearchAction(){
		List<String> titles = borrowerSearchForm.getTitle();
		List<String> authors = borrowerSearchForm.getAuthor();
		List<String> subjects = borrowerSearchForm.getSubject();
		List<Book> results = borrowerService.search(titles, authors, subjects);
		borrowerSearchForm.setResult(results);
		//TODO actually searches
	}
	
	public void performLoginAction(){
		boolean error = false;
		try {
			borrower = borrowerService.getAccount(borrowerLoginForm.getBid(), borrowerLoginForm.getPassword());
		} catch (AbstractLDBException e) {
			e.printStackTrace();
			e.displayError();
			error = true;
			borrower = null;
		}
		if(!error) IoC.displayCompleteMessage("Login Successful!");
	}

	@Override
	public void cleanUp() {
		borrowerSearchForm = new BorrowerSearchForm();
		borrowerLoginForm = new BorrowerLoginForm();
		borrower = new Borrower();
	}

	public void performHoldRequestAction() {
		System.out.println("Placing hold for Bid: " + borrower.getBid() + "\nWith " + borrower.getHoldRequestStrings().size() + " hold requests");
		borrowerService.placeHoldRequest(borrower.getBid(), borrower.getHoldRequestStrings());
	}

	public void performPayFineAction() {
		String fid = borrower.getFid();
		borrowerService.payFine(fid);
		IoC.displayCompleteMessage("Fine Paied");
	}

}
